package com.ruikong.dazhi.controller.base;

import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.model.input.ChangePasswordInput;
import com.ruikong.dazhi.model.input.LoginInput;
import com.ruikong.dazhi.service.base.ILoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = "/data")
@EnableAutoConfiguration
@Api(tags = "0.0登录/退出")
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ILoginService loginService;

    @ApiOperation(value = "登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResult Login(HttpServletRequest request, HttpSession httpSession, @RequestBody LoginInput inputLogin) {
        BaseResult result = new BaseResult();
        try{
            int effect = loginService.checkLogin(inputLogin.getAccount(),inputLogin.getPassword(),result,httpSession,request);
            if (effect > 0) {
                result.setCode(ResultApi.successCode);
            } else {
                result.setCode(ResultApi.failCode);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("login", e.getMessage());
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.loginFailMsg);
        }
        return result;
    }

    @ApiOperation(value = "退出")
    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    public BaseResult LoginOut(HttpSession httpSession, HttpServletRequest request) {
        BaseResult result = new BaseResult();
        try {
            loginService.loginOut(httpSession, request);
            result.setCode(ResultApi.successCode);
            result.setMsg("已退出");
        }catch (Exception e){
            result.setCode(ResultApi.failCode);
            result.setMsg("操作失败");
            logger.error("loginOut", e.getMessage());
        }
        return result;
    }



    @RequestMapping(value = "/changePassword")
    public BaseResult changePassword(@RequestBody ChangePasswordInput changePasswordInput) {
        BaseResult result = new BaseResult();
        try {
            int effect = loginService.changePassword(result,changePasswordInput);
            if(effect > 0){
                result.setCode(ResultApi.successCode);
            } else {
                result.setCode(ResultApi.failCode);
            }
        } catch (Exception e) {
            logger.error("changePassword {}", e.getMessage());
        }
        return result;
    }

}
